package xiangya.bysj.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import xiangya.bysj.entity.HistoryTake;
import xiangya.bysj.entity.Medic;
import xiangya.bysj.entity.TakeCycle;
import xiangya.bysj.entity.TakeInfo;
import xiangya.bysj.mapper.interf.CacheMapper;
import xiangya.bysj.service.impl.HistoryTakeServiceImpl;
import xiangya.bysj.service.impl.MedicServiceImpl;
import xiangya.bysj.service.impl.PatientServiceImpl;
import xiangya.bysj.service.impl.TakeInfoServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:xiangya/bysj/xml/xiangya-spring-config.xml" })
public class Junit4Test {

	@Resource
	private PatientServiceImpl patientService;

	@Resource
	private HistoryTakeServiceImpl historyTakeService;

	@Autowired
	private TakeInfoServiceImpl takeInfoService;

	@Resource
	private CacheMapper cacheMapper;

	@Autowired
	private MedicServiceImpl medicService;

	@Test
	public void testCreateTakeInfo() throws Exception {

		TakeInfo takeinfo = new TakeInfo();
		takeinfo.setMedicId(1);
		takeinfo.setMedicName("敌敌畏");
		takeinfo.setPatientId(2);
		takeinfo.setPatientName("小小");
		takeinfo.setTkReNum(100);
		takeinfo.setTkTime("睡前");
		takeinfo.setTkTimeEat("饭后");
		List<TakeCycle> list = new ArrayList<TakeCycle>();
		TakeCycle cycle = new TakeCycle();
		cycle.setCycleEnd("2016-4-20");
		cycle.setCycleStart("2016-3-21");
		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		map.put("07:30", 3);
		map.put("12:30", 2);
		map.put("18:30", 1);
		cycle.setTkTiNum(JsonUtil.getJson(map));
		list.add(cycle);
		takeinfo.setTkCycle(list);

		takeInfoService.save(takeinfo);
	}

	@Test
	public void testAlert() throws Exception {
		String res = "{\"histId\":43,\"patientId\":1,\"patientName\":\"小小\",\"medicId\":42,\"medicName\":\"氨酪酸片\",\"histDate\":\"2016-04-04\",\"histTime\":\"15:02\",\"histNum\":2,\"histStatus\":0,\"histRecord\":null,\"tkId\":8}";
		HistoryTake h = (HistoryTake) JsonUtil.getObject(res, HistoryTake.class);
		System.out.println(h.toString());
	}

	public void print(BB bb) {
		System.out.println(bb);
	}

	static class BB {
		private String name;
		private int age;

		public BB() {
			super();
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		@Override
		public String toString() {
			return "BB [age=" + age + ", name=" + name + "]";
		}

	}

	@Test
	public void UserSaveTest() throws Exception {
		try {
			Method m = Junit4Test.class.getMethod("print", BB.class);
			m.invoke(this, JsonUtil.getObject("{\"name\":\"abc\",\"age\":20}", BB.class));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Test
	public void historyProduce() throws Exception {
		historyTakeService.produceBatch();
	}

	@Test
	public void testSelectMedic() throws Exception {
		List<Medic> medics = medicService.getByName("大");
		System.out.println(medics.size());
		System.out.println(medicService.getByName("大") == null ? true : false);
	}
}
